<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('业态类型')" prop="typeFormats">
            <dict-select
              v-model="form.typeFormats"
              dictionary="type_formats"
              :placeholder="$t('业态类型')"
              @change="sumTotal"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总可售面积(㎡)')" prop="totalSaleableArea">
            <Money v-model="form.totalSaleableArea" :placeholder="$t('总可售面积')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('累计认购面积(㎡)')" prop="cumulativeSubscriptionArea">
            <Money v-model="form.cumulativeSubscriptionArea" :placeholder="$t('如果是车位则单位是个数')" @change="average" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('累计认购货值')+ $t('金额单位')" prop="cumulativeSubscriptionValue">
            <Money v-model="form.cumulativeSubscriptionValue" unit="w" :placeholder="$t('累计认购货值')" @change="average" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('平均认购单价(元/㎡)')" prop="averageSubscriptionPrice">
            <Money v-model="form.averageSubscriptionPrice" :placeholder="$t('自动计算')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本月认购面积(㎡)')" prop="subscriptionAreaMonth">
            <Money v-model="form.subscriptionAreaMonth" :placeholder="$t('本月认购面积')" @change="MonthPrice" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本月认购货值')+ $t('金额单位')" prop="thisMonthValue">
            <Money v-model="form.thisMonthValue" unit="w" :placeholder="$t('本月认购货值')" @change="MonthPrice" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本月认购单价(元/㎡)')" prop="thisMonthPrice">
            <Money v-model="form.thisMonthPrice" :placeholder="$t('自动计算')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('累计签约面积(㎡)')" prop="accumulatedContractedArea">
            <Money v-model="form.accumulatedContractedArea" :placeholder="$t('如果是车位则单位是个数')" @change="unitPrice" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('累计签约货值')+ $t('金额单位')" prop="accumulatedContractValue">
            <Money v-model="form.accumulatedContractValue" unit="w" :placeholder="$t('累计签约货值')" @change="unitPrice" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('签约单价(元/㎡)')" prop="contractUnitPrice">
            <Money v-model="form.contractUnitPrice" :placeholder="$t('自动计算')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本月签约面积(㎡)')" prop="contractAreaMonth">
            <Money v-model="form.contractAreaMonth" :placeholder="$t('本月签约面积')" @change="priceMonth" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本月签约货值')+ $t('金额单位')" prop="contractValueMonth">
            <Money v-model="form.contractValueMonth" unit="w" :placeholder="$t('本月签约货值')" @change="priceMonth" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本月签约单价(元/㎡)')" prop="contractUnitPriceMonth">
            <Money v-model="form.contractUnitPriceMonth" :placeholder="$t('自动计算')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn />
    </div>
  </div>
</template>

<script>
  import { getTypeFormatsBySum } from '@/api/deal/estate/monthlyMonitoring/dealMonthlyMonitoring'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                cumulativeSubscriptionArea: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                cumulativeSubscriptionValue: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                averageSubscriptionPrice: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                subscriptionAreaMonth: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                thisMonthValue: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                thisMonthPrice: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                accumulatedContractedArea: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                accumulatedContractValue: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                contractUnitPrice: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                contractAreaMonth: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                contractValueMonth: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                contractUnitPriceMonth: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                }
            }
        },
      mounted() {
        this.$bus.$emit('async_done')
      },
        created() {
            this.getDicts('type_formats').then(response => {
                this.typeFormatsOptions = response.data
            })
        },
        methods: {
          init(data) {
            this.loading = true
            if (data != null) {
              this.form = data
              this.form.id = undefined
                this.loading = false
            }
          },
          initAdd() {
            this.form = {
              id: undefined,

              monitoringId: undefined,

              typeFormats: undefined,

              totalSaleableArea: undefined,

              cumulativeSubscriptionArea: undefined,

              cumulativeSubscriptionValue: undefined,

              averageSubscriptionPrice: undefined,

              subscriptionAreaMonth: undefined,

              thisMonthValue: undefined,

              thisMonthPrice: undefined,

              accumulatedContractedArea: undefined,

              accumulatedContractValue: undefined,

              contractUnitPrice: undefined,

              contractAreaMonth: undefined,

              contractValueMonth: undefined,

              contractUnitPriceMonth: undefined,

              remark: undefined,

              delFlag: undefined,

              createBy: undefined,

              createTime: undefined,

              updateBy: undefined,

              updateTime: undefined

            }
            this.form.id = '1'
            this.loading = false
          },
          /** 提交按钮 */
          submitForm: function() {
            this.$refs['form'].validate(valid => {
              if (valid) {
                this.loading = true
                this.msgSuccess(this.$t('保存成功'))
                if (this.form.id === '1') {
                const row = this.form
                  this.$parent.$parent.$parent.form.dealMonitoringDetailList.push(row)
                // this.$emit('DetailList', row)
                }
                this.closeForm()
                this.loading = false
              } else {
                this.locationError(document)
              }
            })
          },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,

                    monitoringId: undefined,

                    typeFormats: undefined,

                    totalSaleableArea: undefined,

                    cumulativeSubscriptionArea: undefined,

                    cumulativeSubscriptionValue: undefined,

                    averageSubscriptionPrice: undefined,

                    subscriptionAreaMonth: undefined,

                    thisMonthValue: undefined,

                    thisMonthPrice: undefined,

                    accumulatedContractedArea: undefined,

                    accumulatedContractValue: undefined,

                    contractUnitPrice: undefined,

                    contractAreaMonth: undefined,

                    contractValueMonth: undefined,

                    contractUnitPriceMonth: undefined,

                    remark: undefined,

                    delFlag: undefined,

                    createBy: undefined,

                    createTime: undefined,

                    updateBy: undefined,

                    updateTime: undefined

                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页
              this.$parent.$parent.$parent.formOpen = false
            },
          // 计算平均认购单价
          average() {
            this.form.averageSubscriptionPrice = this.transZero(this.form.cumulativeSubscriptionValue) / this.transZero(this.form.cumulativeSubscriptionArea)
            if (isNaN(this.form.averageSubscriptionPrice)) {
              this.form.averageSubscriptionPrice = 0
            }
            if (!isFinite(this.form.averageSubscriptionPrice)) {
              this.form.averageSubscriptionPrice = 0
            }
          },
          // 计算本月认购单价
          MonthPrice() {
            this.form.thisMonthPrice = this.transZero(this.form.thisMonthValue) / this.transZero(this.form.subscriptionAreaMonth)
            if (isNaN(this.form.thisMonthPrice)) {
              this.form.thisMonthPrice = 0
            }
            if (!isFinite(this.form.thisMonthPrice)) {
              this.form.thisMonthPrice = 0
            }
          },
          // 计算签约单价
          unitPrice() {
            this.form.contractUnitPrice = this.transZero(this.form.accumulatedContractValue) / this.transZero(this.form.accumulatedContractedArea)
            if (isNaN(this.form.contractUnitPrice)) {
              this.form.contractUnitPrice = 0
            }
            if (!isFinite(this.form.contractUnitPrice)) {
              this.form.contractUnitPrice = 0
            }
          },
          // 计算本月签约单价
          priceMonth() {
            this.form.contractUnitPriceMonth = this.transZero(this.form.contractValueMonth) / this.transZero(this.form.contractAreaMonth)
            if (isNaN(this.form.contractUnitPriceMonth)) {
              this.form.contractUnitPriceMonth = 0
            }
            if (!isFinite(this.form.contractUnitPriceMonth)) {
              this.form.contractUnitPriceMonth = 0
            }
          },
          transZero(val) {
            if (val !== undefined && val !== null && val !== '') {
              return val
            }
            return 0
          },
          // 总可售面积从最新版本的关键指标自动带出
          sumTotal() {
            if (this.form.typeFormats !== undefined && this.form.typeFormats !== null && this.form.typeFormats !== '') {
              getTypeFormatsBySum(this.$route.query.id, this.form.typeFormats).then((response) => {
                if (response.code === 200) {
                  this.form.totalSaleableArea = response.msg
                }
              })
            }
          }
        }
    }
</script>
